package com.crp.unitalinforbaseplat.app.sys.service.impl

import com.crp.unitalinforbaseplat.app.core.Message
import com.crp.unitalinforbaseplat.app.core.MessageUtil
import com.crp.unitalinforbaseplat.app.sys.dao.PermissionMapper
import com.crp.unitalinforbaseplat.app.sys.dao.RolePermissionMapper
import com.crp.unitalinforbaseplat.app.sys.dao.UserMapper
import com.crp.unitalinforbaseplat.app.sys.dao.UserRoleMapper
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.PageBean
import com.crp.unitalinforbaseplat.app.sys.entity.Permission
import com.crp.unitalinforbaseplat.app.sys.entity.User
import com.crp.unitalinforbaseplat.app.sys.entity.UserRole
import com.crp.unitalinforbaseplat.app.sys.service.UserService
import com.crp.unitalinforbaseplat.app.core.utils.MD5Util
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @program: squarenavigationsys
 * @description:用户服务实现类
 * @author:  chenzhicai
 * @create: 2018-11-03-20-38
 **/
@Service
open class UserServiceceImpl(@Autowired val userMapper: UserMapper
) : UserService {
    @Autowired
    lateinit var userRoleMapper: UserRoleMapper

    @Autowired
    lateinit var rolePermissionMapper: RolePermissionMapper

    @Autowired
    lateinit var permissionMapper: PermissionMapper

    @Transactional(readOnly = true)
    override fun verify(userName: String): User {
        if (!userName!!.isNullOrEmpty()) {
            return userMapper.selectByName(userName)
        } else return User()
    }

    @Transactional(readOnly = false)
    override fun regist(user: User): Message {
        if (!user.username!!.isNullOrEmpty()) {
            val dbUser = userMapper.selectByName(user.username)
            if (dbUser == null) {
                userMapper.insertSelective(user)
                return MessageUtil.createMessage(true, "创建成功！")
            } else return MessageUtil.createMessage(false, "该用户名已经被创建了！")
        } else {
            return MessageUtil.createMessage(false, "服务器异常！")
        }
    }

    @Transactional(readOnly = false)
    override fun updatePassword(user: User): Message {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @Transactional(readOnly = true)
    override fun getUserPermission(userId: String): List<Permission> {
        val RoleList = userRoleMapper.selectByUserId(userId)
        val RolePermissionList = rolePermissionMapper.selectByRoleList(RoleList)
        return permissionMapper.selectByRolePermissionList(RolePermissionList)
    }

    @Transactional(readOnly = true)
    override fun list(pageBean: PageBean<User>): PageBean<User> {
        pageBean.setData(userMapper.selectAllUsers(pageBean))
        return pageBean
    }


    //通过ID删除用户
    @Transactional(readOnly = false)
    override fun deleteById(id: Int): Message {
        //先删除user_role表中的记录，在删除user表中的记录
        val user = userMapper.selectByPrimaryKey(id)
        userRoleMapper.deleteByUserId(user.userId)
        userMapper.deleteByPrimaryKey(id)
        return MessageUtil.createMessage(true,"删除成功！")
    }

    @Transactional(readOnly = true)
    override fun selectById(id: Int): User {
        return  userMapper.selectByPrimaryKey(id)
    }

    //添加用户
    @Transactional(readOnly = false)
    override fun edit(user: User): Message {
        userMapper.updateByPrimaryKeySelective(user)
        return MessageUtil.createMessage(true,"编辑用户成功！")
    }

    @Transactional(readOnly = false)
    override fun add(user: User): Message {
        user.status=1
        user.password= MD5Util.getMD5String(user.username,user.password)
        //将用户添加到role表中
        userMapper.insert(user)

        return MessageUtil.createMessage(true,"添加用户成功!")
    }
}
